package kr.hkit.summary_java.erp.serviceUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JDesktopPane;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.GridLayout;

import javax.swing.BoxLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.SQLException;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.summary_java.erp.dao.CodeDAO;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Department;
import kr.hkit.summary_java.erp.dto.Employee;
import javax.swing.JTextField;

public class TestFrame2 extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JMenu menu_emp;
	private JMenu menu_dept;
	private JMenu menu_title;
	private JMenuItem mnEmpAdd;
	private JMenuItem mnEmpDel;
	private JMenuItem mnEmpUpdate;
	private JMenuItem mnEmpSearch;
	private JMenuItem mnDeptAdd;
	private JMenuItem mnDeptDel;
	private JMenuItem mnDeptUpdate;
	private JMenuItem mnDeptSearch;
	private JMenuItem mnTitleAdd;
	private JMenuItem mnTitleDel;
	private JMenuItem mnTitleUpdate;
	private JMenuItem mnTitleSearch;
	private JPanel panel;
	private JPanel panel_1;
	private JButton btnOk;
	private JButton btnCancel;
	private PanelEmployeeUI panel_emp;
	private PanelDepartmentUI panel_dept;
	private PanelTitleUI panel_title;
	private int mn = 0;
	Connection con;
	DaoInterface dao;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestFrame2 frame = new TestFrame2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestFrame2() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		con = ConnectionFactory.getConnection();// 연결접속

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		menu_emp = new JMenu("사원");
		menuBar.add(menu_emp);

		mnEmpAdd = new JMenuItem("추가");
		mnEmpAdd.addActionListener(this);
		menu_emp.add(mnEmpAdd);

		mnEmpDel = new JMenuItem("삭제");
		mnEmpDel.addActionListener(this);
		menu_emp.add(mnEmpDel);

		mnEmpUpdate = new JMenuItem("수정");
		mnEmpUpdate.addActionListener(this);
		menu_emp.add(mnEmpUpdate);

		mnEmpSearch = new JMenuItem("검색");
		mnEmpSearch.addActionListener(this);
		menu_emp.add(mnEmpSearch);

		menu_dept = new JMenu("부서");
		menuBar.add(menu_dept);

		mnDeptAdd = new JMenuItem("추가");
		mnDeptAdd.addActionListener(this);
		menu_dept.add(mnDeptAdd);

		mnDeptDel = new JMenuItem("삭제");
		mnDeptDel.addActionListener(this);
		menu_dept.add(mnDeptDel);

		mnDeptUpdate = new JMenuItem("수정");
		mnDeptUpdate.addActionListener(this);
		menu_dept.add(mnDeptUpdate);

		mnDeptSearch = new JMenuItem("검색");
		mnDeptSearch.addActionListener(this);
		menu_dept.add(mnDeptSearch);

		menu_title = new JMenu("직책");
		menuBar.add(menu_title);

		mnTitleAdd = new JMenuItem("추가");
		mnTitleAdd.addActionListener(this);
		menu_title.add(mnTitleAdd);

		mnTitleDel = new JMenuItem("삭제");
		mnTitleDel.addActionListener(this);
		menu_title.add(mnTitleDel);

		mnTitleUpdate = new JMenuItem("수정");
		mnTitleUpdate.addActionListener(this);
		menu_title.add(mnTitleUpdate);

		mnTitleSearch = new JMenuItem("검색");
		mnTitleSearch.addActionListener(this);
		menu_title.add(mnTitleSearch);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);

		panel_emp = new PanelEmployeeUI();
		panel_emp.setVisible(false);
		panel.add(panel_emp);

		panel_dept = new PanelDepartmentUI();
		panel_dept.setVisible(false);
		panel.add(panel_dept);

		panel_title = new PanelTitleUI();
		panel_title.setVisible(false);
		panel.add(panel_title);

		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		btnOk = new JButton("완료");
		btnOk.addActionListener(this);
		panel_1.add(btnOk);

		btnCancel = new JButton("취소");
		panel_1.add(btnCancel);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnOk) {
			actionPerformedBtnOk(e);
		}
		if (e.getSource() == mnTitleSearch) {
			actionPerformedMnTitle(e);
			mn = 12;
		}
		if (e.getSource() == mnTitleUpdate) {
			actionPerformedMnTitle(e);
			mn = 11;
		}
		if (e.getSource() == mnTitleDel) {
			actionPerformedMnTitle(e);
			mn = 10;
		}
		if (e.getSource() == mnTitleAdd) {
			actionPerformedMnTitle(e);
			mn = 9;
		}
		if (e.getSource() == mnDeptSearch) {
			actionPerformedMnDept(e);
			setEditfalse();
			mn = 8;
		}
		if (e.getSource() == mnDeptUpdate) {
			actionPerformedMnDept(e);
			mn = 7;
		}
		if (e.getSource() == mnDeptDel) {
			actionPerformedMnDept(e);
			mn = 6;
		}
		if (e.getSource() == mnDeptAdd) {
			actionPerformedMnDept(e);
			setEditfalse();
			mn = 5;
		}
		if (e.getSource() == mnEmpSearch) {
			actionPerformedMnEmp(e);
			setEditfalse();
			mn = 4;
		}
		if (e.getSource() == mnEmpUpdate) {
			actionPerformedMnEmp(e);
			mn = 3;
		}
		if (e.getSource() == mnEmpDel) {
			actionPerformedMnEmp(e);
			setEditfalse();
			mn = 2;
		}
		if (e.getSource() == mnEmpAdd) {
			actionPerformedMnEmp(e);
			mn = 1;
		}
	}

	private void setEditfalse() {
		panel_emp.getPanel_3().setEditable(false);
		panel_emp.getPanel_4().setEditable(false);
		panel_emp.getPanel_5().setEditable(false);
		panel_emp.getPanel_6().setEditable(false);

		panel_dept.getpFloor().setEditable(false);
	}

	protected void actionPerformedMnEmp(ActionEvent e) {
		panel_emp.setVisible(true);
		panel_dept.setVisible(false);
		panel_title.setVisible(false);
		dao = new EmployeeDAO(con);
	}

	protected void actionPerformedMnDept(ActionEvent e) {
		panel_emp.setVisible(false);
		panel_dept.setVisible(true);
		panel_title.setVisible(false);
		dao = new DepartmentDAO(con);
	}

	protected void actionPerformedMnTitle(ActionEvent e) {
		panel_emp.setVisible(false);
		panel_dept.setVisible(false);
		panel_title.setVisible(true);
		dao = new CodeDAO(con);
	}

	private Employee input(boolean all) {
		
		int empno, title, manager, sal, deptno;
		String name;
		
		empno = Integer.parseInt(panel_emp.getPanel_1().getTf().getText().trim());
		name = panel_emp.getPanel_2().getTf().getText();

		Employee emp = new Employee(empno, name);
		if (all) {
			title = Integer.parseInt(panel_emp.getPanel_3().getTf().getText());
			emp.setTitle(title);
			manager = Integer.parseInt(panel_emp.getPanel_4().getTf().getText());
			emp.setManager(manager);
			sal = Integer.parseInt(panel_emp.getPanel_5().getTf().getText());
			emp.setSalary(sal);
			deptno = Integer.parseInt(panel_emp.getPanel_6().getTf().getText());
			emp.setDeptNo(deptno);
		}

		return emp;
	}

	protected void actionPerformedBtnOk(ActionEvent e) {
		Employee emp;

		switch (mn) {
		case 1:// 입력
			emp = input(true);
			JOptionPane.showMessageDialog(this, emp);

			try {
				dao.insertItem(emp);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			}
			break;
		case 2:// 삭제
			emp = input(false);
			JOptionPane.showMessageDialog(this, emp);

			try {
				dao.deleteItembyNo(emp);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			}
			break;
		case 3:// 수정
			emp = input(true);
			JOptionPane.showMessageDialog(this, emp);

			try {
				dao.updateItembyNo(emp);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			}
			break;
		case 4:// 검색
			emp = input(false);
			JOptionPane.showMessageDialog(this, emp);

			try {
				dao.searchItem(emp);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			}
			break;
		case 5:
			/*int dno = Integer.parseInt(panel_dept.getpDeptno().getTf().trim());
			name = panel_dept.getpDeptName().getTf();
			int floor = Integer.parseInt(panel_dept.getpFloor().getTf());

			Department d = new Department(dno, name, floor);
			JOptionPane.showMessageDialog(this, d);

			try {
				dao.insertItem(d);
				JOptionPane.showMessageDialog(this, "입력되었습니다");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			}*/
			break;
		case 6:
			break;
		case 7:
			break;
		case 8:
			break;
		case 9:
			break;
		case 10:
			break;
		case 11:
			break;
		case 12:
			break;
		}
	}
}
